﻿/* 
┏━━━━━━━━━━━＼／━━━━━━━━━━━┓      
┃┏━━━━━━━━━━━━━━━━━━━━━━┓┃
     ------------------------------------------    
       Author           : TengSea   
       Created          : 6/24/2021                              
       Last Modified By : TengSea                                 
       Last Modified On : Mouth-Day-Year                                                               
       Description      : FUCKTHEREGULATIONS
     __________________________________________
     Copyright (c) TengSea. All rights reserved.
 ┃┗━━━━━━━━━━━━━━━━━━━━━━┛┃                            
 ┗━━━━━━━━━∪━━━━∪━━━━━━━━━┛
 */

using Hhmocon.Mes.Repository.Repository;
using Hhmocon.Mes.Util.String;
using System;
using System.Collections.Generic;

namespace Hhmocon.Mes.Application.Base
{
    /// <summary>
    /// 应用层所有能用泛型简化的代码
    /// 具有复用性的  删改查   都可以由这个类做掉
    /// </summary>
    public class PikachuApp
    {
        private PikachuRepository _pikachuRepository;
        private SqlAssemble _sqlAssemble;
        public PikachuApp(PikachuRepository pikachuRepository,SqlAssemble sqlAssemble)
        {
            _pikachuRepository = pikachuRepository;
            _sqlAssemble = sqlAssemble;
        }

        /// <summary>
        /// 根据id获取单个数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public T GetById<T>(string id)
        {
            return _pikachuRepository.Get_byId<T>(id);
        }

        /// <summary>
        /// 根据Ids获取全部数据
        /// </summary>
        /// <typeparam name=""></typeparam>
        /// <returns></returns>
        public List<T> GetAllById<T>(string[] ids)
        {
            return _pikachuRepository.Get_byIds<T>(ids);
        }

        /// <summary>
        /// 根据Code获取单个数据(假查询)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="code"></param>
        /// <returns></returns>
        public T Get_ByCode<T>(string code)
        {
            return _pikachuRepository.Get_byCode<T>(code);
        }

        public T Get_ByName<T>(string name)
        {
            return _pikachuRepository.Get_byName<T>(name);
        }

        /// <summary>
        /// 根据条件获取全部数据(接的是假查询)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<T> GetAll<T>(PageReq req, ref long icount)
        {
            string strKey = req.key;
            string strSort = req.sort;
            string strOrder = req.order;
            string whereStr = CommonHelper.GetSqlConditonalStr(strKey);
            string ordStr = string.Empty;
            if (!string.IsNullOrEmpty(whereStr))
            {
                whereStr = "WHERE " + whereStr;
            }

            if (!string.IsNullOrEmpty(strOrder))
            {
                ordStr = strOrder;
                if (!string.IsNullOrEmpty(strSort))
                {
                    ordStr = "ORDER BY " + ordStr + " " + strSort;
                }
                else
                {
                    ordStr = "ORDER BY " + ordStr + " ";
                }
            }

            SqlAssemble sqlAssemble = new ();
            whereStr = "WHERE " + sqlAssemble.Delete_Mark_Assemble(whereStr);
            Console.WriteLine(whereStr);

            icount = _pikachuRepository.GetCount<T>(whereStr);
            return _pikachuRepository.GetAll<T>(whereStr, ordStr);
        }

        /// <summary>
        /// 根据条件获取分页数据
        /// </summary>
        /// <param name="req"></param>
        /// <param name="icount"></param>
        /// <returns></returns>
        public List<T> GetList<T>(PageReq req, ref long icount)
        {
            string strKey = req.key;
            int iPage = req.page;
            int iRows = req.rows;
            string strSort = req.sort;
            string strOrder = req.order;
            string whereStr = CommonHelper.GetSqlConditonalStr(strKey);
            string ordStr = string.Empty;

            if (!string.IsNullOrEmpty(whereStr))
            {
                whereStr = "WHERE " + whereStr;
            }

            if (!string.IsNullOrEmpty(strOrder))
            {
                ordStr = strOrder;
                if (!string.IsNullOrEmpty(strSort))
                {
                    ordStr = "ORDER BY " + ordStr + " " + strSort;
                }
                else
                {
                    ordStr = "ORDER BY " + ordStr + " ";
                }
            }

            //假查询部分
            SqlAssemble sqlAssemble = new(); 
            whereStr = "WHERE" + sqlAssemble.Delete_Mark_Assemble(whereStr);
            Console.WriteLine(whereStr);

            icount = _pikachuRepository.GetCount<T>(whereStr);
            return _pikachuRepository.GetList<T>(iPage, iRows, whereStr, ordStr);
        }

       
      


        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public bool Update<T>(T data)
        {
            return _pikachuRepository.Update(data);
        }

        /// <summary>
        /// 删除数据 真删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete<T>(string[] id)
        {
            return _pikachuRepository.Delete<T>(id);
        }

        /// <summary>
        /// 删除数据 假删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete_Mask<T>(string[] id)
        {
            return _pikachuRepository.Delete_Mask<T>(id);
        }

        #region 为树的枝和叶分别是两种类型做的封装
        /// <summary>
        /// 查找所有parentname is null
        /// </summary>
        /// <returns></returns>
        public List<T> get_RootAndBranch<T>()
        {
            List<T> rootAndBranch = new();
            string className = _sqlAssemble.getClassName<T>();
            string sql;
            switch (className)
            {
                case "base_material_type":
                    //查找父名为根名
                    sql = string.Format("where material_type_parentname is null");
                    break;
                default:
                    sql = "";
                    break;
            }

            //查找根
            List<T> root = _pikachuRepository.Get_bySql<T>(sql);

            if (root == null)
            {
                Console.WriteLine("搜索不到根");
            }
            else
            {
                //遍历根
                foreach (T r in root)
                {
                    rootAndBranch.Add(r);//添加
                                         //查找枝
                    Search_Branch(r,ref rootAndBranch);
                }

            }
            return rootAndBranch;
        }

        /// <summary>
        /// 查找枝
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="r"></param>
        /// <param name="rootAndBranch"></param>
        /// <returns></returns>
        public String Search_Branch<T>(T r,ref List<T> rootAndBranch)
        {
            dynamic r1 = r;
            string className = _sqlAssemble.getClassName<T>();
            string sql="";
            switch (className)
            {
                case "base_material_type":
                    //查找父名为根名
                    sql = string.Format("where material_type_parentname = '{0}'", r1.material_type_name);
                    break;
            }

            List<T> branch = _pikachuRepository.Get_bySql<T>(sql);
            if (branch == null)
            {
                return "传进来的类型名是末枝";
            }
            //如果不是末枝
            else
            {
                foreach (T temp in branch)
                {
                    rootAndBranch.Add(temp);
                    Search_Branch(temp,ref rootAndBranch);
                }
                return "查找下一层";
            }

        }

       
        #endregion




    }
}
